package com.zhongkun.datahome.service.res.standard;

import com.zhongkun.datahome.service.domain.DecorationStandard;
import com.zhongkun.datahome.service.domain.DecorationStandardItem;
import com.zhongkun.datahome.service.domain.MaterialRoom;
import com.zhongkun.datahome.service.res.sku.SkuVo;
import com.zhongkun.datahome.service.utils.StreamUtil;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: lifengsheng
 * @Date: 2025/4/17 13:41
 * @Desc
 */
@Data
public class DecorationStandardDetailVo extends DecorationStandard {
    @ApiModelProperty("空间分组")
    private List<RoomVo> roomList;

    @Data
    public static class RoomVo {
        @ApiModelProperty("空间名称")
        private String room;

        @ApiModelProperty("清单")
        private List<? extends DecorationStandardItem> itemList;

        public static RoomVo of(String room, List<? extends DecorationStandardItem> itemList) {
            RoomVo roomVo = new RoomVo();
            roomVo.setRoom(room);
            roomVo.setItemList(itemList);
            return roomVo;
        }
    }

    public static DecorationStandardDetailVo of(DecorationStandard decorationStandard) {
        DecorationStandardDetailVo decorationStandardDetailVo = new DecorationStandardDetailVo();
        decorationStandardDetailVo.setId(decorationStandard.getId());
        decorationStandardDetailVo.setCreateBy(decorationStandard.getCreateBy());
        decorationStandardDetailVo.setCreateTime(decorationStandard.getCreateTime());
        decorationStandardDetailVo.setUpdateBy(decorationStandard.getUpdateBy());
        decorationStandardDetailVo.setUpdateTime(decorationStandard.getUpdateTime());
        decorationStandardDetailVo.setStandardName(decorationStandard.getStandardName());

        return decorationStandardDetailVo;
    }

    public static DecorationStandardDetailVo of(DecorationStandard decorationStandard,
                                                List<DecorationStandardItem> decorationStandardItems, Map<Long, SkuVo> skuMap) {
        DecorationStandardDetailVo detailVo = DecorationStandardDetailVo.of(decorationStandard);

        List<RoomVo> roomList = new ArrayList<>();
        detailVo.setRoomList(roomList);

        List<DecorationStandardItemSkuVo> itemList = new ArrayList<>();
        decorationStandardItems.forEach(item -> itemList.add(DecorationStandardItemSkuVo.convert(item, skuMap.get(item.getMaterialId()))));

        Map<String, List<DecorationStandardItemSkuVo>> roomItemMap = StreamUtil.listGroupBy(itemList, DecorationStandardItemSkuVo::getRoom);

        roomItemMap.forEach((room, itemItemList) -> roomList.add(RoomVo.of(room, itemItemList)));
        return detailVo;
    }
}
